class Declaration < ActiveRecord::Base

  #Foreign key is niet nodig, maar wordt wel aangeraden
  belongs_to :owner, :class_name => 'User', :foreign_key => 'user_id'
  has_many :declaration_payers, :attributes => true, :dependent => :delete_all #Wanneer de declaratie wordt verwijderd, ook deze verwijderen
  has_many :users, :through => :declaration_payers
  
  validates_presence_of :user_id
  validates_numericality_of :user_id
  validates_length_of :amount, :minimum => 1, :message => "^Het bedrag moet groter zijn dan %d cent"
  validates_presence_of :amount
  validates_numericality_of :amount
  validates_presence_of :description, :message => "^Omschrijving moet ingevuld zijn"
  #validates_number_of :declaration_payers, :message => '^Je moet wel betalers aan deze declaratie koppelen!' 
  
  #Duurde te lang, is vervangen door een cronjob: script/runner -e development 'User.send_mail_for_users_with_high_debts'
  #after_save 'User.send_mail_for_users_with_high_debts'
  
  after_create :send_mail_about_new_declaration
  before_validation :remove_blank_declaration_payers
  
  acts_as_money :amount
  
  def self.find_latest(limit = 5)
    self.find :all, :limit => limit, :order => "created_at desc"
  end
  
  def number_of_payers
    self.declaration_payers.length
  end
  
  def additional_persons
    additional_persons = 0
    
    for x in self.declaration_payers
      additional_persons += x.additional_persons
    end
    
    return additional_persons
  end
  
  def my?
    return self.user_id == $my.id ? true : false
  end
  
  def self.find_by_description_like(what, description, options = {})
    options[:order] ||= "created_at DESC"
  
    self.with_scope :find => { :conditions => ['description LIKE ?', "%#{description}%"] } do
      self.find( what, options )
    end
  end
  
  def editable?(user)
    if self.user_id == user.id
      true
    else
      false
    end
  end
  
  def has_user?(user)
    if user.class == User
      user_id = user.id
    end
    
    begin
      self.declaration_payers.each { |key, value|    
        if key.user.id == user_id
          return true
        end
      }
    rescue
      #Do nothing, because nothing is needed
    end
      
    return false
  end
  
  def is_declaration_payer?(user)
    self.users.include? user
  end
  
  def non_payers
    User.get_all_active - self.users
  end
  
  def empty_declaration_payers
    dps = Array.new
    
    self.non_payers.each do |user|
      dps << DeclarationPayer.new(:user => user)
    end
    
    return dps
  end
  
  def share(user_id)
    #mijn gedeelte:
    #Totale bedrag / totaal_aantal_personen
    #Dat maal mijn aantal personen
    
    total_amount = self.amount
    
    return 0 if total_amount.to_i == 0
    
    total_number_of_persons = 0
    current_user_total_persons = 0
    
    for declaration_payer in self.declaration_payers
      
      total_number_of_persons += 1 + declaration_payer.additional_persons
      
      if declaration_payer.user_id == user_id.to_i
        current_user_total_persons = 1 + declaration_payer.additional_persons
      end
    end
    
    share = (total_amount.to_i.to_f / total_number_of_persons.to_f) * current_user_total_persons.to_i
    
    return Money.new(share)
  end
  
  def testshares
    total = self.amount.to_i
    users = User.find :all
    
    print total.to_s + "\n"
    
    for user in users
      share = self.share(user.id).to_f
      total -= share

    end
    
    return total.round
  end
  
private

  def remove_blank_declaration_payers
    declaration_payers.delete_if { |dp| not dp.do_not_remove_before_save }
  end
  
  def send_mail_about_new_declaration
    users = User.get_all_active
    
    users.each do |user|
      Mailer.deliver_notify_about_declaration(user, self) if user.mail_declaration? and user != self.owner
    end
  end
  
end